/**
	Editor 1.0
	@param  objId:编辑textarea id, toolsObjId:工具条id, toolsArr:工具选项(["B","I","U","code","img","video"])
*/
function Editor(objId,toolsObjId,toolsArr,height){
	var editor_obj=document.getElementById(objId);
	var editor_toolsObj=document.getElementById(toolsObjId);
	var editor_uploadedId=objId+"_iu";
	
	var linkContainer=null,imgContainer=null;
	
	if(height) editor_obj.style.height=height+"px";
	//initialize toolsbar
	editor_toolsObj.innerHTML="";
	if(toolsArr.length==0){
		editor_toolsObj.style.display="none";
	}else{
		for(var i=0;i<toolsArr.length;i++){
			var aLink=document.createElement("a");
			aLink.href="javascript:void(0)";
			var mark=toolsArr[i].toLowerCase();
			switch(mark){
				case 'link':
					var rangeData={start:0,end:0,text:""};
					var linkUlink=document.createElement("u");
					linkUlink.innerHTML="链接";
					linkUlink.onclick=function(){
						rangeData=getRangeData(editor_obj); //获取链接位置、文字
						if(rangeData.text==null || rangeData.text==""){
							alert("请先选中待链接的文字");
						}else{
							if(imgContainer) imgContainer.style.display="none";
							if(videoContainer) videoContainer.style.display="none";
							linkContainer.style.display="block";
						}
					};
					aLink.appendChild(linkUlink);
					
					linkContainer=document.createElement("div");
					linkContainer.className="Container";
					aLink.appendChild(linkContainer);
					//链接地址
					var linkNetwork=document.createElement("div");
					linkNetwork.appendChild(document.createTextNode("链接地址："));
					var linkNetInput=document.createElement("input");
					linkNetInput.value="";
					linkNetInput.style.width="240px";
					linkNetwork.appendChild(linkNetInput);
					linkContainer.appendChild(linkNetwork);
					//link button
					var linkButton=document.createElement("div");
					linkButton.className="Button";
					linkContainer.appendChild(linkButton);
					var linkBtnSure=document.createElement("input");
					linkBtnSure.type="button";
					linkBtnSure.value="确定";
					linkBtnSure.onclick=function(){
						if(linkNetInput.value==""){
							alert("请填写链接地址");
						}else{
							if(!(/^https?:\/\//.test(linkNetInput.value))) linkNetInput.value="http://"+linkNetInput.value;
							InsertAtCursor(editor_obj,"[link href=\""+linkNetInput.value+"\"]","[/link]",rangeData);
							linkNetInput.value="";
							linkContainer.style.display="none";
						}
					}
					linkButton.appendChild(linkBtnSure);
					var linkBtnCancel=document.createElement("input");
					linkBtnCancel.type="button";
					linkBtnCancel.value="取消";
					linkButton.appendChild(linkBtnCancel);
					linkBtnCancel.onclick=function(){ linkContainer.style.display="none"; };
					break;
				case 'code':
					aLink.innerHTML="&lt;code&gt;";
					aLink.onclick=(function(mark){ return function(){InsertMark(mark);} })(mark);
					break;
				case 'img':
					var imgUlink=document.createElement("span");
					imgUlink.innerHTML="图片";
					imgUlink.onclick=function(){
						if(linkContainer) linkContainer.style.display="none";
						if(videoContainer) videoContainer.style.display="none";
						imgContainer.style.display="block";
					};
					aLink.appendChild(imgUlink);
					//img container
					imgContainer=document.createElement("div");
					imgContainer.className="Container";
					aLink.appendChild(imgContainer);
					//uploaded image
					var imgIframe=document.createElement("iframe");
					imgIframe.className="imgIframe";
					imgIframe.src="/index.php?ac=upload";
					imgIframe.marginWidth="0";
					imgIframe.marginHeight="0";
					imgIframe.frameBorder="0";
					imgIframe.scrolling="no";
					imgContainer.appendChild(imgIframe);
					//network image
					var imgNetwork=document.createElement("div");
					imgNetwork.appendChild(document.createTextNode("地址："));
					var imgNetInput=document.createElement("input");
					imgNetInput.id=editor_uploadedId;
					imgNetInput.value="";
					imgNetInput.style.width="267px";
					imgNetwork.appendChild(imgNetInput);
					imgContainer.appendChild(imgNetwork);
					//image description
					var imgDesc=document.createElement("div");
					imgDesc.appendChild(document.createTextNode("描述："));
					var imgDescInput=document.createElement("input");
					imgDescInput.id=editor_uploadedId+"_bn";
					imgDescInput.value="";
					imgDescInput.style.width="267px";
					imgDescInput.style.marginTop="8px";
					imgDesc.appendChild(imgDescInput);
					imgContainer.appendChild(imgDesc);
					//image upload type
					var imgUploadType=document.createElement("input");
					imgUploadType.type="hidden";
					imgUploadType.style.display="none";
					imgUploadType.id=editor_uploadedId+"_t";
					imgUploadType.value=0;
					imgContainer.appendChild(imgUploadType);
					//image button
					var imgButton=document.createElement("div");
					imgButton.className="Button";
					imgContainer.appendChild(imgButton);
					var imgBtnSure=document.createElement("input");
					imgBtnSure.type="button";
					imgBtnSure.value="确定";
					imgBtnSure.onclick=function(){
						if(imgNetInput.value==""){
							alert("请选择图片上传或填写图片地址");
						}else{
							if(imgUploadType.value==0 && !(/^http:\/\//.test(imgNetInput.value))) imgNetInput.value="http://"+imgNetInput.value;
							
							InsertAtCursor(editor_obj,'[img]'+imgNetInput.value+'[/img]');
							imgNetInput.value="";
							imgUploadType.value=0;
							imgDescInput.value="";
							imgContainer.style.display="none";
						}
					}
					imgButton.appendChild(imgBtnSure);
					var imgBtnCancel=document.createElement("input");
					imgBtnCancel.type="button";
					imgBtnCancel.value="取消";
					imgButton.appendChild(imgBtnCancel);
					imgBtnCancel.onclick=function(){ imgContainer.style.display="none"; };
					break;
				case "video":
					var videoUlink=document.createElement("span");
					videoUlink.innerHTML="视频";
					videoUlink.onclick=function(){
						if(linkContainer) linkContainer.style.display="none";
						if(imgContainer) imgContainer.style.display="none";
						videoContainer.style.display="block";
					};
					aLink.appendChild(videoUlink);
					//video container
					videoContainer=document.createElement("div");
					videoContainer.className="Container";
					aLink.appendChild(videoContainer);
					//video地址
					var videoNetwork=document.createElement("div");
					videoNetwork.appendChild(document.createTextNode("视频地址："));
					var videoNetInput=document.createElement("input");
					videoNetInput.value="";
					videoNetInput.style.width="240px";
					videoNetwork.appendChild(videoNetInput);
					videoContainer.appendChild(videoNetwork);
					//video button
					var videoButton=document.createElement("div");
					videoButton.className="Button";
					videoContainer.appendChild(videoButton);
					var videoBtnSure=document.createElement("input");
					videoBtnSure.type="button";
					videoBtnSure.value="确定";
					videoBtnSure.onclick=function(){
						if(videoNetInput.value==""){
							alert("请填写视频地址(.swf)");
						}else{
							if(!(/\.swf$/.test(videoNetInput.value))){
								alert("目前仅支持.swf格式");
								return false;
							}
							if(!(/^http:\/\//.test(videoNetInput.value))) videoNetInput.value="http://"+videoNetInput.value;
							InsertAtCursor(editor_obj,"[video href=\""+videoNetInput.value+"\"/]");
							videoNetInput.value="";
							videoContainer.style.display="none";
						}
					}
					videoButton.appendChild(videoBtnSure);
					var videoBtnCancel=document.createElement("input");
					videoBtnCancel.type="button";
					videoBtnCancel.value="取消";
					videoButton.appendChild(videoBtnCancel);
					videoBtnCancel.onclick=function(){ videoContainer.style.display="none"; };
					break;
				default:
					aLink.innerHTML="<"+mark+">"+toolsArr[i]+"</"+mark+">";
					aLink.onclick=(function(mark){ return function(){InsertMark(mark);} })(mark);
					break;
			}
			editor_toolsObj.appendChild(aLink);
		}
	}
	/* functions */
	function InsertMark(mark){
		InsertAtCursor(editor_obj,"["+mark+"]","[/"+mark+"]");
	}
	function getRangeData(obj){
		var range={start:0,end:0,text:""};
		if(document.selection){
			var sel=document.selection.createRange();
			range.text=sel.text;
			range.bookmark=sel.getBookmark();
		}else if(obj.selectionStart||obj.selectionStart=="0"){
			range.start=obj.selectionStart;
			range.end=obj.selectionEnd;
			range.text=obj.value.substring(range.start,range.end);
		}
		return range;
	}
	function InsertAtCursor(obj,value,endValue,rangeData){
		if(document.selection){
			obj.focus();
			var sel=document.selection.createRange();
			if(rangeData!=null && rangeData.bookmark!=null) sel.moveToBookmark(rangeData.bookmark);
			sel.text=endValue ? value+sel.text+endValue : sel.text+value;
			sel.select();
		}else if(obj.selectionStart||obj.selectionStart=="0"){
			var startPos=obj.selectionStart;
			var endPos=obj.selectionEnd;
			var restoreTop=obj.scrollTop;
			var sel=obj.value.substring(startPos,endPos);
			obj.value=obj.value.substring(0,startPos)+(endValue ? value+sel+endValue : sel+value)+obj.value.substring(endPos,obj.value.length);
			if(restoreTop>0) obj.scrollTop=restoreTop;
			obj.focus();
			obj.selectionStart=startPos+value.length;
			obj.selectionEnd=startPos+value.length;
		}else{
			obj.value+=value;
			obj.focus();
		}
	}
}